package com.lv.service.repo

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.room.*

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * App的公共业务基础数据类
 */

//3、database
@Database(entities = [HomeSearch::class], version = 1, exportSchema = false)
abstract class HomeSearchDatabase : RoomDatabase() {

    abstract val homeSearchDao: HomeSearchDao

    companion object {

        private const val CNIAO_DB_NAME = "home_search_db"

        @Volatile
        private var instance: HomeSearchDatabase? = null

        @Synchronized
        fun getInstance(context: Context): HomeSearchDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                HomeSearchDatabase::class.java,
                CNIAO_DB_NAME
            ).build().also { instance = it }
        }
    }
}

//1、entity的定义

@Entity(tableName = "tb_home_search")
data class HomeSearch(
    @PrimaryKey
    var _id: String,//主键
    var name: String?,
    var search: Boolean,
)
////1、entity的定义
//
//@Entity(tableName = "tb_cniao_user")
//data class CniaoUserInfo(
//    @PrimaryKey
//    val id: Int,//主键
//    val course_permission: Boolean,
//    val token: String?,
//    @Embedded
//    val user: User?
//) {
//    @Keep
//    data class User(
//        @ColumnInfo(name = "cniao_user_id")
//        val id: Int,//用户id
//        val logo_url: String?,//用户头像
//        val reg_time: String?,//用户注册时间
//        val username: String?//用户名
//    )
//}

//2、dao层的定义
@Dao
interface HomeSearchDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertHomeSearch(homeSearch: HomeSearch)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateHomeSearch(homeSearch: HomeSearch)

    @Delete
    fun deleteHomeSearch(homeSearch: HomeSearch)

    @Query("DELETE FROM tb_home_search")
    suspend fun deleteAllHomeSearch()

    // = , in , like
    @Query("select * from tb_home_search where name=:name")
    fun queryLiveHomeSearch(name: String): LiveData<HomeSearch>

    @Query("select * from tb_home_search where name=:name")
    fun queryHomeSearch(name: String): HomeSearch?

    @Query("select * from tb_home_search")
    suspend fun getAllHomeSearch(): List<HomeSearch>
}



